﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using SalaryChannel.WiredNetService;
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.Text.RegularExpressions;
using System.IO;

namespace SalaryChannel
{
    public partial class WiredNetOrder : UserControl
    {
        private WiredNetServiceClient _netService = null;
        private static OperationContext _netPC = null;
        public WiredNetServiceClient NetService
        {
            get
            {
                if (_netService == null)
                {
                    _netService = new WiredNetServiceClient();

                    OperationContext.Current = new OperationContext((IContextChannel)_netService.InnerChannel);
                    MessageHeader header = MessageHeader.CreateHeader("uafcztga", "", ServiceInstance.UserName);
                    OperationContext.Current.OutgoingMessageHeaders.Add(header);
                    header = MessageHeader.CreateHeader("xufasft", "", ServiceInstance.Password);
                    OperationContext.Current.OutgoingMessageHeaders.Add(header);
                    _netPC = OperationContext.Current;
                }
                else
                {
                    OperationContext.Current = _netPC;
                }
                return _netService;
            }
            set { _netService = value; }
        }
        public WiredNetOrder()
        {
            InitializeComponent();
            this.Loaded += new RoutedEventHandler(WiredNetOrder_Loaded);
            NetService.GetAreaNameCompleted += new EventHandler<GetAreaNameCompletedEventArgs>(NetService_GetAreaNameCompleted);
            NetService.GetDistrictByNameCompleted += new EventHandler<GetDistrictByNameCompletedEventArgs>(NetService_GetDistrictByNameCompleted);
            NetService.RegisterNewDistrictCompleted += new EventHandler<RegisterNewDistrictCompletedEventArgs>(NetService_RegisterNewDistrictCompleted);

           // PhoneRegex = new Regex(DataHelper.PhoneNumRegexString);

            NetService.GetRegDistrictCompleted += new EventHandler<GetRegDistrictCompletedEventArgs>(NetService_GetRegDistrictCompleted);
            NetService.DeleteDistrictCompleted += new EventHandler<DeleteDistrictCompletedEventArgs>(NetService_DeleteDistrictCompleted);
            NetService.UpdateDistrictCompleted += new EventHandler<UpdateDistrictCompletedEventArgs>(NetService_UpdateDistrictCompleted);
            NetService.CreateNewDistrictCompleted += new EventHandler<CreateNewDistrictCompletedEventArgs>(NetService_CreateNewDistrictCompleted);
            NetService.GetWlanHotCompleted += new EventHandler<GetWlanHotCompletedEventArgs>(NetService_GetWlanHotCompleted);
            NetService.UpdateWlanHotCompleted += new EventHandler<UpdateWlanHotCompletedEventArgs>(NetService_UpdateWlanHotCompleted);
        }

       

       
       // static Regex PhoneRegex;

        
        void WiredNetOrder_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
               
                PropertyCBox4.Items.Add("新建");
                PropertyCBox4.Items.Add("成熟");
                BulidSts4.Items.Add("未签约");
                BulidSts4.Items.Add("待建");
                BulidSts4.Items.Add("建设中");
                BulidSts4.Items.Add("已竣工");
            }
            catch 
            {
                App.MessageBox.ShowDialog("连接服务器失败");
            }
        }

        void NetService_GetAreaNameCompleted(object sender, GetAreaNameCompletedEventArgs e)
        {
            try
            {
                if (e.Result == null)
                {
                    App.MessageBox.ShowDialog("查询县区信息失败");
                }
                else
                {
                    AreaCBox.Items.Clear();
                    AreaCBox4.Items.Clear();
                    if (WlanMngPage.Visibility == Visibility.Visible)
                    {
                        WlanAreaCBox.Items.Clear();
                    }
                    foreach (string s in e.Result)
                    {
                        AreaCBox.Items.Add(s);
                        AreaCBox4.Items.Add(s);
                        if (WlanMngPage.Visibility == Visibility.Visible)
                        {
                            WlanAreaCBox.Items.Add(s);
                        }
                    }

                }
            }
            catch 
            {
                
               App.MessageBox.ShowDialog("查询县区信息失败");
            }
        }

        public void Initiallize()
        {
            ClearPage0();
            ClearPage1();
            ClearPage2();
            ClearPage3();
            ClearPage5();
            ClearPage4();
            IsMngPageReadOnly = true;

            ManagePage.Visibility = App.IsAuth(19) ? Visibility.Visible : Visibility.Collapsed;
            WlanMngPage.Visibility = App.IsAuth(20) ? Visibility.Visible : Visibility.Collapsed;
            if (AreaCBox.Items.Count == 0)
            {
                try
                {
                    NetService.GetAreaNameAsync(App.CurrentUser.CityCode);
                }
                catch 
                {
                    
                }
            }
        }

        private void ClearPage2()
        {
            ReStart.Text = DateTime.Now.ToShortDateString();
            ReEnd.Text = ReStart.Text;
            RegGrid.ItemsSource = null;
        }

        private void ClearPage3()
        {
            DisName4.Text = "请在小区查询中选择一个小区，或点击新增";
            if (AreaCBox4.Items.Count > 0)
            {
                AreaCBox4.SelectedIndex = 0;
            }
            if (PropertyCBox4.Items.Count > 0)
            {
                PropertyCBox4.SelectedIndex = 0;
            }
            CoverUsers4.Text = string.Empty;
            if (BulidSts4.Items.Count > 0)
            {
                BulidSts4.SelectedIndex = 0;
            }
            ExpectDate.Text = DateTime.Now.ToShortDateString();
            CompletedDate.Text = DateTime.Now.AddMonths(3).ToShortDateString();
            MngNewBtn.IsEnabled = true;
            MngUpdateBtn.IsEnabled = false;
                MngCmitBtn.IsEnabled = false;
                MngDelBtn.IsEnabled = false;
            
        }

        private void ClearPage1()
        {
            if (AreaCBox.Items.Count > 0)
            {
                AreaCBox.SelectedIndex = 0;
            }
            NewDisNameTB.Text = string.Empty;
            UserNameTB.Text = string.Empty;
            UserTelTB.Text = string.Empty;
            AddressTB.Text = string.Empty;
        }

        #region 小区查询
        private void ClearPage0()
        {
            DistrictNameTB.Text = string.Empty;
            DistrictGrid.ItemsSource = null;
        }

        private void SearchBtn_Click(object sender, RoutedEventArgs e)
        {
            if (string.IsNullOrEmpty(DistrictNameTB.Text.Trim())
                || DistrictNameTB.Text.Trim() == "小区"
                || DistrictNameTB.Text.Length < 2)
            {
                App.MessageBox.ShowDialog("请输入有效地关键字");
                return;
            }

            try
            {
                NetService.GetDistrictByNameAsync(DistrictNameTB.Text.Trim());
                SearchBtn.IsEnabled = false;
                loading0.Show();
            }
            catch
            {
                App.MessageBox.ShowDialog("连接服务器失败");
            }
            finally
            {
                ClearPage3();
                DistrictGrid.ItemsSource = null;
            }
        }
        void NetService_GetDistrictByNameCompleted(object sender, GetDistrictByNameCompletedEventArgs e)
        {
            try
            {
                if (e.Result == null)
                {
                    App.MessageBox.ShowDialog("查询失败");
                }
                else if (e.Result.Count == 0)
                {
                    App.MessageBox.ShowDialog("没有查询到数据");
                }
                else
                {
                    DistrictGrid.ItemsSource = e.Result;
                }
            }
            catch (Exception ex)
            {

                App.MessageBox.ShowDialog("查询失败," + ex.Message);
            }
            finally
            {
                SearchBtn.IsEnabled = true;
                loading0.Stop();
            }
        }

      

        private void DistrictGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (DistrictGrid.SelectedItem != null)
            {
                if (ManagePage.Visibility == Visibility.Visible)
                {
                    Wired_Net_District dis = DistrictGrid.SelectedItem as Wired_Net_District;
                    DisName4.Text = dis.District_Name;
                    DisName4.Tag = dis.ID;
                    AreaCBox4.SelectedItem = dis.Area_Name;
                    PropertyCBox4.SelectedItem = dis.District_Attribute;
                    CoverUsers4.Text = dis.District_Cover_Users.ToString();
                    BulidSts4.SelectedItem = dis.Build_Status;
                    ExpectDate.Text = dis.Expected_Completed_Date == null ? string.Empty : dis.Expected_Completed_Date.Value.ToShortDateString();
                    CompletedDate.Text = dis.Completed_Date == null ? string.Empty : dis.Completed_Date.Value.ToShortDateString();

                    IsMngPageReadOnly = true;
                    MngDelBtn.IsEnabled = true;
                    MngUpdateBtn.IsEnabled = true;
                }
            }
        }

        #endregion

        #region 小区登记
        private void ClearBtn_Click(object sender, RoutedEventArgs e)
        {
            ClearPage1();

        }

        private void NewBtn_Click(object sender, RoutedEventArgs e)
        {
            if (AreaCBox.SelectedItem == null
                || string.IsNullOrEmpty(NewDisNameTB.Text)
                || string.IsNullOrEmpty(UserNameTB.Text)
                || string.IsNullOrEmpty(UserTelTB.Text)
                || string.IsNullOrEmpty(AddressTB.Text)
                )
            {
                App.MessageBox.ShowDialog("请完事填写所有信息");
                return;
            }
            //if (!PhoneRegex.IsMatch(UserTelTB.Text))
            //{
            //    App.MessageBox.ShowDialog("手机号码不合法");
            //    return;
            //}
            try
            {
                string area=AreaCBox.SelectedItem.ToString();

                NetService.RegisterNewDistrictAsync(area.Substring(6),
                        NewDisNameTB.Text, UserNameTB.Text, UserTelTB.Text, AddressTB.Text,
                        App.CurrentUser.UserID);
                NewBtn.IsEnabled = false;
            }
            catch 
            {

                App.MessageBox.ShowDialog("连接服务器失败");
            }
        }

        void NetService_RegisterNewDistrictCompleted(object sender, RegisterNewDistrictCompletedEventArgs e)
        {
            try
            {
                if (e.Result)
                {
                    App.MessageBox.ShowDialog("操作成功");
                }
                else
                {
                    App.MessageBox.ShowDialog("操作失败");
                }
            }
            catch
            {
                App.MessageBox.ShowDialog("操作失败");
            }
            finally
            {
                NewBtn.IsEnabled = true;
            }
        }

        #endregion
        #region 小区登记查询
        private void ReQueryBtn_Click(object sender, RoutedEventArgs e)
        {
            if (DateTime.Parse(ReStart.Text) > DateTime.Parse(ReEnd.Text))
            {
                App.MessageBox.ShowDialog("开始日期不能大于结束日期");
                return;
            }

            DateTime start, end;
            try
            {
                start = DateTime.Parse(ReStart.Text + " 00:00:00");
                end = DateTime.Parse(ReEnd.Text + " 23:59:59");
                NetService.GetRegDistrictAsync(start, end);
                ReQueryBtn.IsEnabled = false;
                loading2.Show();
            }
            catch 
            {
                App.MessageBox.ShowDialog("连接服务器失败");
            }
        }
        void NetService_GetRegDistrictCompleted(object sender, GetRegDistrictCompletedEventArgs e)
        {
            try
            {
                if (e.Result == null)
                {
                    App.MessageBox.ShowDialog("查询失败");
                }
                else if (e.Result.Count == 0)
                {
                    App.MessageBox.ShowDialog("没有查询到数据");
                }
                else
                {
                    RegGrid.ItemsSource = e.Result;
                }
            }
            catch
            {
                App.MessageBox.ShowDialog("查询失败");
            }
            finally {
                ReQueryBtn.IsEnabled = true;
                loading2.Stop();
            }
        }

        private void ReExportBtn_Click(object sender, RoutedEventArgs e)
        {
            if (RegGrid.ItemsSource == null)
            {
                App.MessageBox.ShowDialog("没有需要保存的数据");
                return;
            }
            try
            {
                List<Wired_Net_Register> list = RegGrid.ItemsSource as List<Wired_Net_Register>;
                SaveFileDialog dialog = new SaveFileDialog();
                dialog.Filter = "TXT文件|*.txt";
                if (dialog.ShowDialog().Value)
                {
                    using (StreamWriter writer = new StreamWriter(dialog.OpenFile(), System.Text.Encoding.Unicode))
                    {
                        string line = string.Empty;
                        line = "序号\t县区\t小区\t登记用户\t用户电话\t详细地址\t登记时间\t登记工号";
                        writer.WriteLine(line);
                        foreach (Wired_Net_Register row in list)
                        {
                            line = string.Format("\"{0}\"\t\"{1}\"\t\"{2}\"\t\"{3}\"\t\"{4}\"\t\"{5}\"\t\"{6}\"\t\"{7}\"",
                                row.ID,
                                row.Area_Name, row.District_Name, row.User_Name, row.User_Tel, row.User_Address,
                                row.Reg_Time, row.Reg_User_ID);
                            writer.WriteLine(line);
                        }
                        writer.Flush();
                    }
                    App.MessageBox.ShowDialog("保存成功");
                }
                
            }


            catch (Exception ex)
            {

                App.MessageBox.ShowDialog("保存失败," + ex.Message);
            }
        }


         #endregion
        #region 小区管理
        bool isMngPageReadOnly = true;

        public bool IsMngPageReadOnly
        {
            get { 
                return isMngPageReadOnly; }
            set
            {
                if (value != isMngPageReadOnly)
                {
                    isMngPageReadOnly = value;
                    DisName4.IsReadOnly = value;
                    AreaCBox4.IsEnabled = !value;
                    PropertyCBox4.IsEnabled = !value;
                    CoverUsers4.IsReadOnly = value;
                    BulidSts4.IsEnabled = !value;
                    ExpectDate.IsEnabled = !value;
                    CompletedDate.IsEnabled = !value;
                }
            }
        }
    
        private void MngNewBtn_Click(object sender, RoutedEventArgs e)
        {
            DisName4.Tag = null;
            ClearPage3();
            IsMngPageReadOnly = false;
            MngNewBtn.IsEnabled = false;
            MngUpdateBtn.IsEnabled = false;
            MngDelBtn.IsEnabled = false;
            MngCmitBtn.IsEnabled = true;
            DisName4.Text = string.Empty;
        }

        private void MngUpdateBtn_Click(object sender, RoutedEventArgs e)
        {
            IsMngPageReadOnly = false;

            MngNewBtn.IsEnabled = false;
            MngUpdateBtn.IsEnabled = false;
            MngDelBtn.IsEnabled = false;
            MngCmitBtn.IsEnabled = true;
        }

        private void MngDelBtn_Click(object sender, RoutedEventArgs e)
        {
            if (DisName4.Tag != null)
            {
                App.MessageBox.ShowDialog("确实要删除此小区吗？", new Action(DoDelete), Title.Warning);
                
            }
            else
            {
                App.MessageBox.ShowDialog("请先选择一个小区");
            }

        }
        void DoDelete()
        {
            try
            {

                NetService.DeleteDistrictAsync(int.Parse(DisName4.Tag.ToString()));
                MngNewBtn.IsEnabled = false;
                MngUpdateBtn.IsEnabled = false;
                MngDelBtn.IsEnabled = false;
                MngCmitBtn.IsEnabled = false;
                loading3.Show();
            }
            catch 
            {
                App.MessageBox.ShowDialog("连接服务器失败"); 
            }
        }

        private void MngCmitBtn_Click(object sender, RoutedEventArgs e)
        {
            if (string.IsNullOrEmpty(DisName4.Text)
                || AreaCBox4.SelectedItem == null
                || PropertyCBox4.SelectedItem == null
            || CoverUsers4.Text == string.Empty
                || BulidSts4.SelectedItem == null)
            {
                App.MessageBox.ShowDialog("请完整填写各项信息");
                return;
            }
            int users = 0;
            if (!int.TryParse(CoverUsers4.Text, out users))
            {
                App.MessageBox.ShowDialog("覆盖用户数必须为整数");
                return;
            }
            if (users < 1)
            {
                App.MessageBox.ShowDialog("覆盖用户数大于0");
                return;
            }

            DateTime? ec, c;
            ec = null;
            c = null;
            if (!(string.IsNullOrEmpty(ExpectDate.Text)
                && string.IsNullOrEmpty(CompletedDate.Text)))
            {
                try
                {
                    ec = DateTime.Parse(ExpectDate.Text);
                    c = DateTime.Parse(CompletedDate.Text);
                    if (ec >= c)
                    {
                        App.MessageBox.ShowDialog("预计竣工日期必须小于竣工日期");
                        return;
                    }
                }
                catch
                {
                    App.MessageBox.ShowDialog("请正确设置日期");
                    return;
                }
            }
            try{

                if (DisName4.Tag == null)
                {
                    NetService.CreateNewDistrictAsync(new Wired_Net_District
                    {
                        District_Name = DisName4.Text,
                        Area_Name = AreaCBox4.SelectedItem.ToString(),
                        District_Attribute = PropertyCBox4.SelectedItem.ToString(),
                        District_Cover_Users = users,
                        Build_Status = BulidSts4.SelectedItem.ToString(),
                        Expected_Completed_Date = ec,
                        Completed_Date = c,
                        Create_User_ID = App.CurrentUser.UserID,
                        Create_Time = DateTime.Now
                    });
                }
                else
                {
                    NetService.UpdateDistrictAsync(new Wired_Net_District
                    {
                        ID=int.Parse(DisName4.Tag.ToString()),
                        District_Name = DisName4.Text,
                        Area_Name = AreaCBox4.SelectedItem.ToString(),
                        District_Attribute = PropertyCBox4.SelectedItem.ToString(),
                        District_Cover_Users = users,
                        Build_Status = BulidSts4.SelectedItem.ToString(),
                        Expected_Completed_Date = ec,
                        Completed_Date = c,
                        Update_User_ID = App.CurrentUser.UserID,
                        Update_Time = DateTime.Now
                    });
                }
                MngDelBtn.IsEnabled = false;
                MngNewBtn.IsEnabled = false;
                MngCmitBtn.IsEnabled = false;
                MngUpdateBtn.IsEnabled = false;
                loading3.Show();
            }
            catch
            {
                App.MessageBox.ShowDialog("连接服务器失败");
            }
        }

        void NetService_CreateNewDistrictCompleted(object sender, CreateNewDistrictCompletedEventArgs e)
        {
            try
            {
                if (e.Result==0)
                {
                    App.MessageBox.ShowDialog("创建成功");

                }
                else if (e.Result == 1)
                {
                    App.MessageBox.ShowDialog("创建失败,不能重复创建");
                }
                else
                {
                    App.MessageBox.ShowDialog("创建失败");
                }
            }
            catch
            {

                App.MessageBox.ShowDialog("创建失败");
            }
            finally
            {
                MngDelBtn.IsEnabled = false;
                MngNewBtn.IsEnabled = true;
                MngCmitBtn.IsEnabled = false;
                MngUpdateBtn.IsEnabled = true;
                loading3.Stop();
            }
        }

        void NetService_UpdateDistrictCompleted(object sender, UpdateDistrictCompletedEventArgs e)
        {
            try
            {
                if (e.Result)
                {
                    App.MessageBox.ShowDialog("更新成功");

                }
                else
                {
                    App.MessageBox.ShowDialog("更新失败");
                }
            }
            catch
            {

                App.MessageBox.ShowDialog("更新失败");
            }
            finally
            {
                MngDelBtn.IsEnabled = false;
                MngNewBtn.IsEnabled = true;
                MngCmitBtn.IsEnabled = false;
                MngUpdateBtn.IsEnabled = true;
                loading3.Stop();
            }
        }

        void NetService_DeleteDistrictCompleted(object sender, DeleteDistrictCompletedEventArgs e)
        {
            try
            {
                if (e.Result)
                {
                    ClearPage3();
                    App.MessageBox.ShowDialog("删除成功");

                }
                else
                {
                    App.MessageBox.ShowDialog("删除失败");
                }
            }
            catch
            {

                App.MessageBox.ShowDialog("删除失败");
            }
            finally
            {
                MngNewBtn.IsEnabled = true;
                MngUpdateBtn.IsEnabled = true;
                MngDelBtn.IsEnabled = true;
                MngCmitBtn.IsEnabled = false;
                loading3.Stop();
            }
        }
        #endregion

        #region WLAN热点查询

        void ClearPage4()
        {
            WlanKeyTB.Text = string.Empty;
            WlanGrid.ItemsSource = null;
            WlanPageIndexTB.Text = "0";
        }

        readonly int PageSize = 20;
        int _pageIndex = 0;

         int PageIndex
        {
            get { return _pageIndex; }
            set 
            {
                if (value != _pageIndex)
                {
                    _pageIndex = value;
                    WlanPageIndexTB.Text = value.ToString();
                    try
                    {
                        NetService.GetWlanHotAsync(WlanKeyTB.Text.Trim(), PageSize, value, Guid.NewGuid());
                        Wlanloading1.Show();
                        WlanGrid.ItemsSource = null;
                    }
                    catch 
                    {
                        App.MessageBox.ShowDialog("连接服务器失败");
                    }
                }
            }
        }




        private void WlanSearchBtn_Click(object sender, RoutedEventArgs e)
        {
            _pageIndex = 0;
            PageIndex = 1;
        }

        /// <summary>
        /// 上一页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void TextBlock_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (PageIndex > 1)
            {
                PageIndex--;
            }
        }
        /// <summary>
        /// 下一页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void TextBlock_MouseLeftButtonDown_1(object sender, MouseButtonEventArgs e)
        {
            try
            {
                if (WlanGrid.ItemsSource != null)
                {
                    if ((WlanGrid.ItemsSource as List<VW_Wlan_Hot>).Count == PageSize)
                    {
                        PageIndex++;
                    }
                }
            }
            catch 
            {
                App.MessageBox.ShowDialog("连接服务器异常");
            }
        }

        private void WlanGoBtn_Click(object sender, RoutedEventArgs e)
        {
            int index = -1;
            if (int.TryParse(WlanPageIndexTB.Text, out index))
            {
                PageIndex = index;
            }
            else
            {
                WlanPageIndexTB.Text = PageIndex.ToString();
            }
        }
       
        void NetService_GetWlanHotCompleted(object sender, GetWlanHotCompletedEventArgs e)
        {
            try
            {
                if (e.Result == null)
                {
                    App.MessageBox.ShowDialog("查询失败，请稍后重试");
                }
                else if (e.Result.Count == 0)
                {
                    App.MessageBox.ShowDialog("没有查询到数据");
                }
                else
                {
                    WlanGrid.ItemsSource = e.Result;
                }
            }
            catch 
            {
                App.MessageBox.ShowDialog("查询失败，请稍后重试");
            }
            finally
            {
                Wlanloading1.Stop();
            }
        }

        
        #endregion

        void ClearPage5()
        {
            HotNameTB.Text = string.Empty;
            HotRemarkTB.Text = string.Empty;
            HotRemarkTB.IsReadOnly = true;
            HotNameTB.IsReadOnly = true;
            UpdateHotBtn.IsEnabled = false;
            NewHotBtn.IsEnabled = true;
            CmtHotBtn.IsEnabled = false;
        }


        private void WlanGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (WlanGrid.SelectedItem != null)
            {
                VW_Wlan_Hot hot = WlanGrid.SelectedItem as VW_Wlan_Hot;
                WlanAreaCBox.SelectedIndex = -1;
                foreach (string a in WlanAreaCBox.Items)
                {
                    if (a.Contains("]" + hot.CountyName))
                    {
                        WlanAreaCBox.SelectedItem = a;
                        break;
                    }
                }
                HotNameTB.Text = hot.Hot;
                HotRemarkTB.Text = hot.Remark;
                WlanMngPage.Tag = hot.ID;

                UpdateHotBtn.IsEnabled = true;
                NewHotBtn.IsEnabled = true;
                CmtHotBtn.IsEnabled = false;
            }
        }

     

        private void UpdateHotBtn_Click(object sender, RoutedEventArgs e)
        {
            HotNameTB.IsReadOnly = false;
            HotRemarkTB.IsReadOnly = false;
            UpdateHotBtn.IsEnabled = false;
            NewHotBtn.IsEnabled = false;
            CmtHotBtn.IsEnabled = true;
        }

        private void NewHotBtn_Click(object sender, RoutedEventArgs e)
        {
            WlanMngPage.Tag = null;
            HotNameTB.IsReadOnly = false;
            HotRemarkTB.IsReadOnly = false;
            HotRemarkTB.Text = string.Empty;
            HotNameTB.Text = string.Empty;

            UpdateHotBtn.IsEnabled = false;
            NewHotBtn.IsEnabled = false;
            CmtHotBtn.IsEnabled = true;
        }

        private void CmtHotBtn_Click(object sender, RoutedEventArgs e)
        {
            if (WlanAreaCBox.SelectedItem == null)
            {
                App.MessageBox.ShowDialog("请先选择一个县区");
                return;
            }

            try
            {                
                NetService.UpdateWlanHotAsync(new DC_Wlan_Hot
                  {
                      ID = WlanMngPage.Tag == null ? -1 : int.Parse(WlanMngPage.Tag.ToString()),
                      Hot = HotNameTB.Text.Trim(),
                      Remark = HotRemarkTB.Text.Trim(),
                      Create_ID = App.CurrentUser.UserID,
                      Create_Date = DateTime.Now,
                      Area_Code = App.GetID(WlanAreaCBox.SelectedItem.ToString()).ToString()

                  });
                UpdateHotBtn.IsEnabled = true;
                NewHotBtn.IsEnabled = true;
                CmtHotBtn.IsEnabled = false;
                HotNameTB.IsReadOnly = true;
                HotRemarkTB.IsReadOnly = true;
                WlanLoading2.Show();
            }
            catch 
            {
                App.MessageBox.ShowDialog("连接服务器失败");
                
            }
        }

        void NetService_UpdateWlanHotCompleted(object sender, UpdateWlanHotCompletedEventArgs e)
        {
            bool succeed = false;
            try
            {
                if (e.Result == 0)
                {
                    succeed = true;
                    App.MessageBox.ShowDialog("操作成功");
                }
                else if (e.Result == 1)
                {
                    App.MessageBox.ShowDialog("更新的热点不存在");
                }
                else if (e.Result == 2)
                {
                    App.MessageBox.ShowDialog("新增的热点已存在");
                }
                else
                {
                    App.MessageBox.ShowDialog("操作失败，请稍后再试");
                }
            }
            catch
            {

                App.MessageBox.ShowDialog("操作失败，请稍后再试");
            }
            finally
            {
                WlanLoading2.Stop();
            }
            if (!succeed)
            {
                UpdateHotBtn.IsEnabled = false;
                NewHotBtn.IsEnabled = false;
                CmtHotBtn.IsEnabled = true;
                HotNameTB.IsReadOnly = false;
                HotRemarkTB.IsReadOnly = false;
            }
            else
            {
                WlanMngPage.Tag = null;
                HotNameTB.Text = string.Empty;
                HotRemarkTB.Text = string.Empty;
            }

        }

       
    }
}
